﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;
using Eassets.Layered.Domain.Global;
using Eassets.Service.Global;

namespace Eassets.UI.GlobalReference
{
    public partial class OrganizationLevelForm : System.Web.UI.Page
    {

        #region Define variables
        public string SelectOrganizationLevelId;
        #endregion


        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAllOrganizationLevels();
                textBoxOrganizationLevelCode.Attributes.Add("onChange", "javascript:this.value=toUpperCase(this.value);");
                textBoxOrganizationLevelCode.Attributes.Add("onKeyPress", "javascript:return validateCodeForSymbols(event)");
                textBoxDescription.Attributes.Add("onChange", "javascript:this.value=toTitleCase(this.value)");

                Session["SelectedButton"] = "PageLoad";
            }
        }

        /// <summary>
        /// Adds the business unit.
        /// </summary>
        private void AddOrganizationLevel()
        {
            OrganizationLevelService businessUnitService = new OrganizationLevelService();
            OrganizationLevel businessUnit = new OrganizationLevel();

            businessUnit.Code = textBoxOrganizationLevelCode.Text.Trim().ToUpper();
            businessUnit.Description = textBoxDescription.Text.Trim();

            try
            {
                if (Page.IsValid)
                {
                    businessUnitService.AddOrganizationLevel(businessUnit);
                    FlashMessage1.Message = "Successfully Saved";
                    FlashMessage1.CssClass = "okmessage";
                    FlashMessage1.Display();
                }
            }
            catch (Exception exception)
            {
                FlashMessage1.Message = "Record Already Exists";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();
            }
        }

        /// <summary>
        /// Modifies the business unit.
        /// </summary>
        private void ModifyOrganizationLevel()
        {
            OrganizationLevelService businessUnitService = new OrganizationLevelService();
            OrganizationLevel businessUnit = new OrganizationLevel();

            businessUnit.Code = textBoxOrganizationLevelCode.Text.Trim().ToUpper();
            businessUnit.Description = textBoxDescription.Text.Trim();
            businessUnit.sys_DateCreation = DateTime.Today;
            businessUnit.sys_DateLastModification = DateTime.Today;
            businessUnit.OrganizationLevelId = Convert.ToInt64(ViewState["SelectOrganizationLevelId"]);
            //businessUnit.OrganizationLevelId = Convert.ToInt64(SelectOrganizationLevelId);

            try
            {
                if (Page.IsValid)
                {
                    businessUnitService.ModifyOrganizationLevel(businessUnit);
                    FlashMessage1.Message = "Successfully Saved";
                    FlashMessage1.CssClass = "okmessage";
                    FlashMessage1.Display();
                }
            }
            catch (Exception exception)
            {
                FlashMessage1.Message = "Saving Failed";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();

            }
        }

        /// <summary>
        /// Deletes the business unit.
        /// </summary>
        private void DeleteOrganizationLevel()
        {
            OrganizationLevelService businessUnitService = new OrganizationLevelService();
            OrganizationLevel businessUnit = new OrganizationLevel();

           businessUnit.OrganizationLevelId = Convert.ToInt64(ViewState["SelectOrganizationLevelId"]);

            try
            {
                if (Page.IsValid)
                {
                    businessUnitService.DeleteOrganizationLevel(businessUnit.OrganizationLevelId);
                    FlashMessage1.Message = "Successfully Deleted";
                    FlashMessage1.CssClass = "okmessage";
                    FlashMessage1.Display();
                }

            }
            catch (Exception exception)
            {
                FlashMessage1.Message = "Deleting Failed";
                FlashMessage1.CssClass = "errormessage";
                FlashMessage1.Display();

            }
        }

        /// <summary>
        /// Gets all business units.
        /// </summary>
        private void GetAllOrganizationLevels()
        {
            OrganizationLevelService businessUnitService = new OrganizationLevelService();
            OrganizationLevel businessUnit = new OrganizationLevel();

            try
            {
                //if (Page.IsValid)
                //{

                gridViewOrganizationLevel.DataSource = businessUnitService.GetAllOrganizationLevel();
                gridViewOrganizationLevel.DataBind();



                //}
            }
            catch (Exception exception)
            {


            }
        }

        private Boolean CheckMandatory()
        {
            if (textBoxOrganizationLevelCode.Text.Trim() == String.Empty)
            {
                FlashMessage1.Display("[Business Unit Code] cannot be blank", "errormessage");
                return false;
            }
            else if (textBoxDescription.Text.Trim() == String.Empty)
            {
                FlashMessage1.Display("[Description] cannot be blank", "errormessage");
                return false;
            }
            return true;
        }

        /// <summary>
        /// Handles the SelectedIndexChanged event of the gridViewOrganizationLevel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void gridViewOrganizationLevel_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = gridViewOrganizationLevel.SelectedRow;

            ViewState["SelectOrganizationLevelId"] = row.Cells[1].Text.Trim();

            textBoxOrganizationLevelCode.Text = row.Cells[2].Text.Trim();
            textBoxDescription.Text = row.Cells[3].Text.Trim();

        }

        /// <summary>
        /// Clears the text box.
        /// </summary>
        protected void ClearTextBox()
        {
            textBoxOrganizationLevelCode.Text = string.Empty;
            textBoxDescription.Text = string.Empty;
            ViewState["SelectOrganizationLevelId"] = string.Empty;
        }

        /// <summary>
        /// Handles the PageIndexChanging event of the gridViewOrganizationLevel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewPageEventArgs"/> instance containing the event data.</param>
        protected void gridViewOrganizationLevel_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GetAllOrganizationLevels();
            gridViewOrganizationLevel.PageIndex = e.NewPageIndex;
            gridViewOrganizationLevel.DataBind();
        }

        protected void gridViewOrganizationLevel_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 1)
            {
                e.Row.Cells[1].Visible = false;
            }
        }

        protected void ButtonAdd_Click(object sender, EventArgs e)
        {
            ClearTextBox();
            textBoxOrganizationLevelCode.Focus();
            Session["SelectedButton"] = "A";
        }

        /// <summary>
        /// Handles the cLick event of the ButtonModify control
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonModify_Click(object sender, EventArgs e)
        {
            textBoxOrganizationLevelCode.Focus();
            Session["SelectedButton"] = "M";
        }

        /// <summary>
        /// Handles the Click event of the buttonDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonDelete_Click(object sender, EventArgs e)
        {
            //if (ViewState["SelectOrganizationLevelId"].ToString() == String.Empty)
            //{
            //    FlashMessage1.Display("No record is selected", "errormessage");
            //}
            //else
            //{
            //    DeleteOrganizationLevel();
            //    GetAllOrganizationLevels();
            //    ClearTextBox();
            //}
            //Session["SelectedButton"] = "D";
        }

        /// <summary>
        /// Handles the Click event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            if (CheckMandatory())
            {
                if (Session["SelectedButton"] == "A")
                {
                    AddOrganizationLevel();
                    GetAllOrganizationLevels();
                    ClearTextBox();
                }

                if (Session["SelectedButton"] == "M")
                {
                    ModifyOrganizationLevel();
                    GetAllOrganizationLevels();
                    ClearTextBox();
                }
            }
        }

        protected void ButtonClear_Click(object sender, EventArgs e)
        {
            ClearTextBox();
            textBoxOrganizationLevelCode.Focus();
        }

    }
}